<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">












<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />






















<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=6.0.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=6.0.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=6.0.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon.ico?v=6.0.4">


  <link rel="mask-icon" href="/images/logo.svg?v=6.0.4" color="#222">









<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '6.0.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":true,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  
  <meta name="keywords" content="百度IFE2017," />


<meta name="description" content="JavaScript和树（二）任务目的熟练JavaScript 学习树这种数据结构的基本知识 任务描述基于任务七，参考示例图，将二叉树变成了多叉树，并且每一个节点中带有内容提供一个按钮，显示开始遍历，点击后，以动画的形式呈现遍历的过程当前被遍历到的节点做一个特殊显示（比如不同的颜色）每隔一段时间（500ms，1s等时间自定）再遍历下一个节点增加一个输入框及一个“查询”按钮，点击按钮时，开始在树中以">
<meta name="keywords" content="百度IFE2017">
<meta property="og:type" content="article">
<meta property="og:title" content="IFE JavaScript Task 1-08">
<meta property="og:url" content="http://yoursite.com/2017/08/22/ife2017/js/Task08/index.html">
<meta property="og:site_name" content="Kamui&#39;s Notes">
<meta property="og:description" content="JavaScript和树（二）任务目的熟练JavaScript 学习树这种数据结构的基本知识 任务描述基于任务七，参考示例图，将二叉树变成了多叉树，并且每一个节点中带有内容提供一个按钮，显示开始遍历，点击后，以动画的形式呈现遍历的过程当前被遍历到的节点做一个特殊显示（比如不同的颜色）每隔一段时间（500ms，1s等时间自定）再遍历下一个节点增加一个输入框及一个“查询”按钮，点击按钮时，开始在树中以">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-02-28T03:43:14.694Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="IFE JavaScript Task 1-08">
<meta name="twitter:description" content="JavaScript和树（二）任务目的熟练JavaScript 学习树这种数据结构的基本知识 任务描述基于任务七，参考示例图，将二叉树变成了多叉树，并且每一个节点中带有内容提供一个按钮，显示开始遍历，点击后，以动画的形式呈现遍历的过程当前被遍历到的节点做一个特殊显示（比如不同的颜色）每隔一段时间（500ms，1s等时间自定）再遍历下一个节点增加一个输入框及一个“查询”按钮，点击按钮时，开始在树中以">



  <link rel="alternate" href="/atom.xml" title="Kamui's Notes" type="application/atom+xml" />




  <link rel="canonical" href="http://yoursite.com/2017/08/22/ife2017/js/Task08/"/>



<script type="text/javascript" id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>
  <title>IFE JavaScript Task 1-08 | Kamui's Notes</title>
  









  <noscript>
  <style type="text/css">
    .use-motion .motion-element,
    .use-motion .brand,
    .use-motion .menu-item,
    .sidebar-inner,
    .use-motion .post-block,
    .use-motion .pagination,
    .use-motion .comments,
    .use-motion .post-header,
    .use-motion .post-body,
    .use-motion .collection-title { opacity: initial; }

    .use-motion .logo,
    .use-motion .site-title,
    .use-motion .site-subtitle {
      opacity: initial;
      top: initial;
    }

    .use-motion {
      .logo-line-before i { left: initial; }
      .logo-line-after i { right: initial; }
    }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"> <div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Kamui's Notes</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页</a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />标签</a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            <i class="menu-item-icon fa fa-fw fa-th"></i> <br />分类</a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档</a>
        </li>
      
        
        <li class="menu-item menu-item-project">
          <a href="/project/" rel="section">
            <i class="menu-item-icon fa fa-fw fa-diamond"></i> <br />项目</a>
        </li>
      
        
        <li class="menu-item menu-item-demo">
          <a href="/demo/" rel="section">
            <i class="menu-item-icon fa fa-fw fa-rocket"></i> <br />演示</a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于</a>
        </li>
      

      
    </ul>
  

  
</nav>


  



 </div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/08/22/ife2017/js/Task08/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Kamui">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Kamui's Notes">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">IFE JavaScript Task 1-08</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-08-22T16:19:19+08:00">2017-08-22</time>
            

            
            

            
          </span>

          

          
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="post-meta-item-icon"
            >
            <i class="fa fa-eye"></i>
             阅读次数： 
            <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>
            </span>
          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="JavaScript和树（二）"><a href="#JavaScript和树（二）" class="headerlink" title="JavaScript和树（二）"></a>JavaScript和树（二）</h1><h2 id="任务目的"><a href="#任务目的" class="headerlink" title="任务目的"></a>任务目的</h2><p>熟练JavaScript</p>
<p>学习树这种数据结构的基本知识</p>
<h2 id="任务描述"><a href="#任务描述" class="headerlink" title="任务描述"></a>任务描述</h2><p>基于任务七，参考示例图，将二叉树变成了多叉树，并且每一个节点中带有内容<br>提供一个按钮，显示开始遍历，点击后，以动画的形式呈现遍历的过程<br>当前被遍历到的节点做一个特殊显示（比如不同的颜色）<br>每隔一段时间（500ms，1s等时间自定）再遍历下一个节点<br>增加一个输入框及一个“查询”按钮，点击按钮时，开始在树中以动画形式查找节点内容和输入框中内容一致的节点，找到后以特殊样式显示该节点，找不到的话给出找不到的提示。查询过程中的展示过程和遍历过程保持一致</p>
<h2 id="任务注意事项"><a href="#任务注意事项" class="headerlink" title="任务注意事项"></a>任务注意事项</h2><p>树的遍历算法和方式自定，但推荐可以提供多种算法的展示（增加多个按钮，每个按钮对应不同的算法）<br>如果按照示例图中展示树，可以使用flexbox布局<br>实现简单功能的同时，请仔细学习JavaScript基本语法、事件、DOM相关的知识<br>请注意代码风格的整齐、优雅<br>代码中含有必要的注释<br>建议不使用任何第三方库、框架</p>
<a id="more"></a>
<p><em>代码如下：</em></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"utf-8"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>IFE JavaScript Task 08<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="undefined"></span></span><br><span class="line"><span class="javascript">    <span class="built_in">window</span>.onload = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">        (<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            <span class="keyword">var</span> tree = <span class="built_in">document</span>.querySelector(<span class="string">"#tree-wrap"</span>),</span></span><br><span class="line"><span class="undefined">                data = [],</span></span><br><span class="line"><span class="javascript">                timer = <span class="literal">null</span>;</span></span><br><span class="line"><span class="javascript">            <span class="keyword">var</span> regNum = <span class="keyword">new</span> <span class="built_in">RegExp</span>(<span class="string">"^[0-9]*$"</span>);<span class="comment">//匹配数字</span></span></span><br><span class="line"><span class="javascript">            <span class="comment">//console.log(tree);测试抓取tree下所有节点</span></span></span><br><span class="line"><span class="javascript">            <span class="comment">//为元素添加样式</span></span></span><br><span class="line"><span class="javascript">            <span class="keyword">const</span> getTreeWrap = <span class="built_in">document</span>.getElementById(<span class="string">"tree-wrap"</span>);</span></span><br><span class="line"><span class="javascript">            <span class="keyword">const</span> getTreeWrapElement = getTreeWrap.getElementsByTagName(<span class="string">"div"</span>);</span></span><br><span class="line"><span class="javascript">            <span class="keyword">const</span> getForms = <span class="built_in">document</span>.getElementById(<span class="string">"forms"</span>);</span></span><br><span class="line"><span class="javascript">            <span class="keyword">const</span> getButtonsElement = getForms.getElementsByTagName(<span class="string">"a"</span>);</span></span><br><span class="line"><span class="javascript">            <span class="keyword">const</span> getInputElement = getForms.getElementsByTagName(<span class="string">"input"</span>);</span></span><br><span class="line"><span class="javascript">            <span class="keyword">const</span> getHr = <span class="built_in">document</span>.getElementById(<span class="string">"hr-line"</span>);</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="javascript">            <span class="comment">//tree下各元素样式</span></span></span><br><span class="line"><span class="javascript">            <span class="keyword">const</span> elementStyle = <span class="string">"display: inline-block;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"background:#ffffff;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"color:#309AE6;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"font-size:12px;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"line-height:normal;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"border:1px solid #309AE6;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"border-radius:3px;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"padding: 6px 10px;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"margin:2px;"</span>;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="javascript">            <span class="comment">//tree下各元素选中样式</span></span></span><br><span class="line"><span class="javascript">            <span class="keyword">const</span> changeStyle = <span class="string">"display: inline-block;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"-webkit-transition: all 0.38s ease;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"transition: all 0.38s ease;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"background:#309AE6;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"box-shadow:2px 2px 3px #cccccc;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"color:#ffffff;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"font-size:12px;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"line-height:normal;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"border:1px solid #309AE6;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"border-radius:3px;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"padding: 6px 10px;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"margin:2px;"</span>;</span></span><br><span class="line"><span class="javascript">            <span class="comment">//tree下各元素搜索选中样式</span></span></span><br><span class="line"><span class="javascript">            <span class="keyword">const</span> selectStyle = <span class="string">"display: inline-block;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"-webkit-transition: all 0.38s ease;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"transition: all 0.38s ease;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"background:#fd6ba6;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"box-shadow:2px 2px 3px #cccccc;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"color:#ffffff;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"font-size:12px;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"line-height:normal;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"border:1px solid #fd6ba6;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"border-radius:3px;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"padding: 6px 10px;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"margin:2px;"</span>;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="javascript">            <span class="comment">//按钮与输入框默认样式</span></span></span><br><span class="line"><span class="javascript">            <span class="keyword">const</span> buttonsStyle = <span class="string">"display: inline-block;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"background:#ffffff;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"color:#fd6ba6;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"font-size:12px;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"text-decoration:none;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"line-height:normal;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"border:1px solid #fd6ba6;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"border-radius:3px;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"padding: 6px 10px;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"margin:0px 5px 0 0;"</span>;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="javascript">            <span class="comment">//按钮hover样式</span></span></span><br><span class="line"><span class="javascript">            <span class="keyword">const</span> buttonsChangeStyle = <span class="string">"display: inline-block;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"background:#fd6ba6;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"color:#ffffff;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"font-size:12px;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"text-decoration:none;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"line-height:normal;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"-webkit-transition: all 0.28s ease;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"transition: all 0.28s ease;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"border-radius:3px;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"border:1px solid #fd6ba6;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"padding: 6px 10px;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"margin:0px 5px 0 0;"</span>;</span></span><br><span class="line"><span class="javascript">            <span class="comment">//hr样式</span></span></span><br><span class="line"><span class="javascript">            <span class="keyword">const</span> hrStyle = <span class="string">"height: 1px;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"background: #dddddd;"</span> +</span></span><br><span class="line"><span class="javascript">                <span class="string">"margin: 10px 0;"</span>;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="javascript">            <span class="comment">//遍历所有子元素增加样式</span></span></span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">addStyleFunc</span>(<span class="params">getElement, style</span>) </span>&#123;</span></span><br><span class="line"><span class="undefined">                getTreeWrap.style.cssText = style;</span></span><br><span class="line"><span class="javascript">                <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; getElement.length; i++) &#123;</span></span><br><span class="line"><span class="javascript">                    <span class="comment">//console.log(getTreeWrapElement[i]);</span></span></span><br><span class="line"><span class="undefined">                    getElement[i].style.cssText = style;</span></span><br><span class="line"><span class="undefined">                &#125;</span></span><br><span class="line"><span class="undefined">            &#125;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="javascript">            addStyleFunc(getButtonsElement, buttonsStyle);<span class="comment">//为按钮增加样式</span></span></span><br><span class="line"><span class="javascript">            addStyleFunc(getInputElement, buttonsStyle);<span class="comment">//为按钮增加样式</span></span></span><br><span class="line"><span class="javascript">            addStyleFunc(getTreeWrapElement, elementStyle);<span class="comment">//为tree下每个元素增加样式</span></span></span><br><span class="line"><span class="javascript">            getHr.style.cssText = hrStyle;<span class="comment">//为hr分割线增加样式</span></span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="javascript">            <span class="comment">//按钮增加hover样式</span></span></span><br><span class="line"><span class="javascript">            getForms.addEventListener(<span class="string">"mouseover"</span>, <span class="function"><span class="keyword">function</span> (<span class="params">event</span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">                <span class="keyword">if</span> (event.target.nodeName === <span class="string">"A"</span>) &#123;</span></span><br><span class="line"><span class="javascript">                    event.target.style.cssText = buttonsChangeStyle;<span class="comment">//为按钮增加样式</span></span></span><br><span class="line"><span class="undefined">                &#125;</span></span><br><span class="line"><span class="undefined">            &#125;);</span></span><br><span class="line"><span class="javascript">            getForms.addEventListener(<span class="string">"mouseout"</span>, <span class="function"><span class="keyword">function</span> (<span class="params">event</span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">                <span class="keyword">if</span> (event.target.nodeName === <span class="string">"A"</span>) &#123;</span></span><br><span class="line"><span class="javascript">                    event.target.style.cssText = buttonsStyle;<span class="comment">//为按钮增加样式</span></span></span><br><span class="line"><span class="undefined">                &#125;</span></span><br><span class="line"><span class="undefined">            &#125;);</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="javascript">            <span class="comment">//按钮事件绑定</span></span></span><br><span class="line"><span class="javascript">            getForms.addEventListener(<span class="string">"click"</span>, <span class="function"><span class="keyword">function</span> (<span class="params">event</span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">                <span class="keyword">if</span> (event.target.innerHTML === <span class="string">"前序遍历"</span>) &#123;</span></span><br><span class="line"><span class="undefined">                    reset();</span></span><br><span class="line"><span class="undefined">                    preOrder(tree);</span></span><br><span class="line"><span class="undefined">                    changeColor();</span></span><br><span class="line"><span class="undefined">                &#125;</span></span><br><span class="line"><span class="javascript">                <span class="keyword">if</span> (event.target.innerHTML === <span class="string">"后序遍历"</span>) &#123;</span></span><br><span class="line"><span class="undefined">                    reset();</span></span><br><span class="line"><span class="undefined">                    postOrder(tree);</span></span><br><span class="line"><span class="undefined">                    changeColor();</span></span><br><span class="line"><span class="undefined">                &#125;</span></span><br><span class="line"><span class="javascript">                <span class="keyword">if</span> (event.target.innerHTML === <span class="string">"前序搜索"</span>) &#123;</span></span><br><span class="line"><span class="javascript">                    <span class="keyword">let</span> getValue = getInputElement[<span class="number">0</span>].value;</span></span><br><span class="line"><span class="javascript">                    <span class="keyword">if</span> (!regNum.test(getValue) || getValue === <span class="string">""</span>) &#123;</span></span><br><span class="line"><span class="javascript">                        alert(<span class="string">"请输入数字"</span>);</span></span><br><span class="line"><span class="javascript">                        getValue = <span class="string">""</span>;</span></span><br><span class="line"><span class="javascript">                    &#125; <span class="keyword">else</span> &#123;</span></span><br><span class="line"><span class="undefined">                        reset();</span></span><br><span class="line"><span class="undefined">                        preOrder(tree);</span></span><br><span class="line"><span class="undefined">                        changeColor(getValue);</span></span><br><span class="line"><span class="undefined">                    &#125;</span></span><br><span class="line"><span class="undefined">                &#125;</span></span><br><span class="line"><span class="javascript">                <span class="keyword">if</span> (event.target.innerHTML === <span class="string">"后序搜索"</span>) &#123;</span></span><br><span class="line"><span class="javascript">                    <span class="keyword">let</span> getValue = getInputElement[<span class="number">0</span>].value;</span></span><br><span class="line"><span class="javascript">                    <span class="keyword">if</span> (!regNum.test(getValue) || getValue === <span class="string">""</span>) &#123;</span></span><br><span class="line"><span class="javascript">                        alert(<span class="string">"请输入数字"</span>);</span></span><br><span class="line"><span class="javascript">                        getValue = <span class="string">""</span>;</span></span><br><span class="line"><span class="javascript">                    &#125; <span class="keyword">else</span> &#123;</span></span><br><span class="line"><span class="undefined">                        reset();</span></span><br><span class="line"><span class="undefined">                        postOrder(tree);</span></span><br><span class="line"><span class="undefined">                        changeColor(getValue);</span></span><br><span class="line"><span class="undefined">                    &#125;</span></span><br><span class="line"><span class="undefined">                &#125;</span></span><br><span class="line"><span class="undefined">            &#125;);</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="javascript">            <span class="comment">// 前序遍历</span></span></span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">preOrder</span>(<span class="params">node</span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">                <span class="keyword">if</span> (node) &#123;</span></span><br><span class="line"><span class="undefined">                    data.push(node);</span></span><br><span class="line"><span class="javascript">                    <span class="keyword">var</span> num = node.children;</span></span><br><span class="line"><span class="javascript">                    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; num.length; i++) &#123;</span></span><br><span class="line"><span class="undefined">                        preOrder(num[i]);</span></span><br><span class="line"><span class="undefined">                    &#125;</span></span><br><span class="line"><span class="undefined">                &#125;</span></span><br><span class="line"><span class="undefined">            &#125;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="javascript">            <span class="comment">// 后序</span></span></span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">postOrder</span>(<span class="params">node</span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">                <span class="keyword">if</span> (node) &#123;</span></span><br><span class="line"><span class="javascript">                    <span class="keyword">var</span> num = node.children;</span></span><br><span class="line"><span class="javascript">                    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; num.length; i++) &#123;</span></span><br><span class="line"><span class="undefined">                        postOrder(num[i]);</span></span><br><span class="line"><span class="undefined">                    &#125;</span></span><br><span class="line"><span class="undefined">                    data.push(node);</span></span><br><span class="line"><span class="undefined">                &#125;</span></span><br><span class="line"><span class="undefined">            &#125;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">changeColor</span>(<span class="params">text</span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">                <span class="keyword">var</span> time;</span></span><br><span class="line"><span class="javascript">                <span class="keyword">var</span> reg = <span class="keyword">new</span> <span class="built_in">RegExp</span>(text + <span class="string">"+"</span>);<span class="comment">//需要匹配内容</span></span></span><br><span class="line"><span class="javascript">                <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; data.length; i++) &#123;</span></span><br><span class="line"><span class="javascript">                    (<span class="function"><span class="keyword">function</span> (<span class="params">j</span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">                        setTimeout(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="undefined">                            data[j].style.cssText = changeStyle;</span></span><br><span class="line"><span class="undefined">                        &#125;, time = 500 * j);</span></span><br><span class="line"><span class="undefined">                    &#125;)(i);</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="javascript">                    (<span class="function"><span class="keyword">function</span> (<span class="params">j</span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">                        setTimeout(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="undefined">                            data[j].style.cssText = elementStyle;</span></span><br><span class="line"><span class="javascript">                            <span class="keyword">if</span> (reg.test(data[j].childNodes[<span class="number">0</span>].nodeValue)) &#123;</span></span><br><span class="line"><span class="undefined">                                data[j].style.cssText = selectStyle;</span></span><br><span class="line"><span class="undefined">                            &#125;</span></span><br><span class="line"><span class="undefined">                        &#125;, time + 500);</span></span><br><span class="line"><span class="undefined">                    &#125;)(i);</span></span><br><span class="line"><span class="undefined">                &#125;</span></span><br><span class="line"><span class="undefined">            &#125;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="javascript">            <span class="comment">// 初始化</span></span></span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">reset</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="undefined">                data = [];</span></span><br><span class="line"><span class="undefined">                addStyleFunc(getTreeWrapElement, elementStyle);</span></span><br><span class="line"><span class="undefined">            &#125;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined">        &#125;)()</span></span><br><span class="line"><span class="undefined">    &#125;;</span></span><br><span class="line"><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"tree-wrap"</span>&gt;</span>1</span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span>&gt;</span>2</span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span>&gt;</span>5</span><br><span class="line">            <span class="tag">&lt;<span class="name">div</span>&gt;</span>10<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">div</span>&gt;</span>11<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">div</span>&gt;</span>12<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">div</span>&gt;</span>13<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span>&gt;</span>6<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span>&gt;</span>7</span><br><span class="line">            <span class="tag">&lt;<span class="name">div</span>&gt;</span>14<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">div</span>&gt;</span>15<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span>&gt;</span>3</span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span>&gt;</span>8</span><br><span class="line">            <span class="tag">&lt;<span class="name">div</span>&gt;</span>16<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">div</span>&gt;</span>17<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">div</span>&gt;</span>18<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span>&gt;</span>9</span><br><span class="line">            <span class="tag">&lt;<span class="name">div</span>&gt;</span>19</span><br><span class="line">                <span class="tag">&lt;<span class="name">div</span>&gt;</span>21<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">div</span>&gt;</span>20<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span>&gt;</span>4<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"hr-line"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"forms"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">"javascript:void(0);"</span>&gt;</span>前序遍历<span class="tag">&lt;/<span class="name">a</span>&gt;</span><span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">"javascript:void(0);"</span>&gt;</span>后序遍历<span class="tag">&lt;/<span class="name">a</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">title</span>=<span class="string">""</span> <span class="attr">type</span>=<span class="string">"text"</span></span></span><br><span class="line"><span class="tag">                                                                                       <span class="attr">placeholder</span>=<span class="string">"请输入数字..."</span>&gt;</span><span class="tag">&lt;<span class="name">a</span></span></span><br><span class="line"><span class="tag">        <span class="attr">href</span>=<span class="string">"javascript:void(0);"</span>&gt;</span>前序搜索<span class="tag">&lt;/<span class="name">a</span>&gt;</span><span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">"javascript:void(0);"</span>&gt;</span>后序搜索<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p><em>Demo如下：</em></p>
<script>
    window.onload = function () {
        (function () {
            var tree = document.querySelector("#tree-wrap"),
                data = [],
                timer = null;
            var regNum = new RegExp("^[0-9]*$");//匹配数字
            //console.log(tree);测试抓取tree下所有节点
            //为元素添加样式
            const getTreeWrap = document.getElementById("tree-wrap");
            const getTreeWrapElement = getTreeWrap.getElementsByTagName("div");
            const getForms = document.getElementById("forms");
            const getButtonsElement = getForms.getElementsByTagName("a");
            const getInputElement = getForms.getElementsByTagName("input");
            const getHr = document.getElementById("hr-line");

            //tree下各元素样式
            const elementStyle = "display: inline-block;" +
                "background:#ffffff;" +
                "color:#309AE6;" +
                "font-size:12px;" +
                "line-height:normal;" +
                "border:1px solid #309AE6;" +
                "border-radius:3px;" +
                "padding: 6px 10px;" +
                "margin:2px;";

            //tree下各元素选中样式
            const changeStyle = "display: inline-block;" +
                "-webkit-transition: all 0.38s ease;" +
                "transition: all 0.38s ease;" +
                "background:#309AE6;" +
                "box-shadow:2px 2px 3px #cccccc;" +
                "color:#ffffff;" +
                "font-size:12px;" +
                "line-height:normal;" +
                "border:1px solid #309AE6;" +
                "border-radius:3px;" +
                "padding: 6px 10px;" +
                "margin:2px;";
            //tree下各元素搜索选中样式
            const selectStyle = "display: inline-block;" +
                "-webkit-transition: all 0.38s ease;" +
                "transition: all 0.38s ease;" +
                "background:#fd6ba6;" +
                "box-shadow:2px 2px 3px #cccccc;" +
                "color:#ffffff;" +
                "font-size:12px;" +
                "line-height:normal;" +
                "border:1px solid #fd6ba6;" +
                "border-radius:3px;" +
                "padding: 6px 10px;" +
                "margin:2px;";

            //按钮与输入框默认样式
            const buttonsStyle = "display: inline-block;" +
                "background:#ffffff;" +
                "color:#fd6ba6;" +
                "font-size:12px;" +
                "text-decoration:none;" +
                "line-height:normal;" +
                "border:1px solid #fd6ba6;" +
                "border-radius:3px;" +
                "padding: 6px 10px;" +
                "margin:0px 5px 3px 0;";

            //按钮hover样式
            const buttonsChangeStyle = "display: inline-block;" +
                "background:#fd6ba6;" +
                "color:#ffffff;" +
                "font-size:12px;" +
                "text-decoration:none;" +
                "line-height:normal;" +
                "-webkit-transition: all 0.28s ease;" +
                "transition: all 0.28s ease;" +
                "border-radius:3px;" +
                "border:1px solid #fd6ba6;" +
                "padding: 6px 10px;" +
                "margin:0px 5px 3px 0;";
            //hr样式
            const hrStyle = "height: 1px;" +
                "background: #dddddd;" +
                "margin: 10px 0;";

            //遍历所有子元素增加样式
            function addStyleFunc(getElement, style) {
                getTreeWrap.style.cssText = style;
                for (let i = 0; i < getElement.length; i++) {
                    //console.log(getTreeWrapElement[i]);
                    getElement[i].style.cssText = style;
                }
            }

            addStyleFunc(getButtonsElement, buttonsStyle);//为按钮增加样式
            addStyleFunc(getInputElement, buttonsStyle);//为按钮增加样式
            addStyleFunc(getTreeWrapElement, elementStyle);//为tree下每个元素增加样式
            getHr.style.cssText = hrStyle;//为hr分割线增加样式

            //按钮增加hover样式
            getForms.addEventListener("mouseover", function (event) {
                if (event.target.nodeName === "A") {
                    event.target.style.cssText = buttonsChangeStyle;//为按钮增加样式
                }
            });
            getForms.addEventListener("mouseout", function (event) {
                if (event.target.nodeName === "A") {
                    event.target.style.cssText = buttonsStyle;//为按钮增加样式
                }
            });


            //按钮事件绑定
            getForms.addEventListener("click", function (event) {
                if (event.target.innerHTML === "前序遍历") {
                    reset();
                    preOrder(tree);
                    changeColor();
                }
                if (event.target.innerHTML === "后序遍历") {
                    reset();
                    postOrder(tree);
                    changeColor();
                }
                if (event.target.innerHTML === "前序搜索") {
                    let getValue = getInputElement[0].value;
                    if (!regNum.test(getValue) || getValue === "") {
                        alert("请输入数字");
                        getValue = "";
                    } else {
                        reset();
                        preOrder(tree);
                        changeColor(getValue);
                    }
                }
                if (event.target.innerHTML === "后序搜索") {
                    let getValue = getInputElement[0].value;
                    if (!regNum.test(getValue) || getValue === "") {
                        alert("请输入数字");
                        getValue = "";
                    } else {
                        reset();
                        postOrder(tree);
                        changeColor(getValue);
                    }
                }
            });

            // 前序遍历
            function preOrder(node) {
                if (node) {
                    data.push(node);
                    var num = node.children;
                    for (var i = 0; i < num.length; i++) {
                        preOrder(num[i]);
                    }
                }
            }

            // 后序
            function postOrder(node) {
                if (node) {
                    var num = node.children;
                    for (let i = 0; i < num.length; i++) {
                        postOrder(num[i]);
                    }
                    data.push(node);
                }
            }

            function changeColor(text) {
                var time;
                var reg = new RegExp(text + "+");//需要匹配内容
                for (let i = 0; i < data.length; i++) {
                    (function (j) {
                        setTimeout(function () {
                            data[j].style.cssText = changeStyle;
                        }, time = 500 * j);
                    })(i);

                    (function (j) {
                        setTimeout(function () {
                            data[j].style.cssText = elementStyle;
                            if (reg.test(data[j].childNodes[0].nodeValue)) {
                                data[j].style.cssText = selectStyle;
                            }
                        }, time + 500);
                    })(i);
                }
            }

            // 初始化
            function reset() {
                data = [];
                addStyleFunc(getTreeWrapElement, elementStyle);
            }

        })()
    };
</script>

<div id="tree-wrap">1<div>2<div>5<div>10</div><div>11</div><div>12</div><div>13</div></div><div>6</div><div>7<div>14</div><div>15</div></div></div><div>3<div>8<div>16</div><div>17</div><div>18</div></div><div>9<div>19<div>21</div></div><div>20</div></div></div><div>4</div></div><div id="hr-line"></div><div id="forms"><a href="javascript:void(0);" target="_blank" rel="noopener">前序遍历</a><a href="javascript:void(0);" target="_blank" rel="noopener">后序遍历</a><input title="" type="text" placeholder="请输入数字..."><a href="javascript:void(0);" target="_blank" rel="noopener">前序搜索</a><a href="javascript:void(0);" target="_blank" rel="noopener">后序搜索</a></div>
      
    </div>

    

    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/百度IFE2017/" rel="tag"># 百度IFE2017</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/08/07/ife2017/js/Task07/" rel="next" title="IFE JavaScript Task 1-07">
                <i class="fa fa-chevron-left"></i> IFE JavaScript Task 1-07
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/08/29/ife2017/js/Task09/" rel="prev" title="IFE JavaScript Task 1-09">
                IFE JavaScript Task 1-09 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/avatar.png"
                alt="Kamui" />
            
              <p class="site-author-name" itemprop="name">Kamui</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">58</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  <a href="/categories/index.html">
                    
                    
                      
                    
                    <span class="site-state-item-count">1</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">16</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
              
                <span class="links-of-author-item">
                  <a href="https://github.com/kamui1073" target="_blank" title="GitHub"><i class="fa fa-fw fa-github"></i>GitHub</a>
                  
                </span>
              
            </div>
          

          
          

          
          

          
            
          
          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#JavaScript和树（二）"><span class="nav-number">1.</span> <span class="nav-text">JavaScript和树（二）</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#任务目的"><span class="nav-number">1.1.</span> <span class="nav-text">任务目的</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#任务描述"><span class="nav-number">1.2.</span> <span class="nav-text">任务描述</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#任务注意事项"><span class="nav-number">1.3.</span> <span class="nav-text">任务注意事项</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Kamui</span>

  

  
</div>




  <div class="powered-by">Hosted by <a href='https://coding.net/help/doc/pages/index.html' style='font-weight:bold;' target='_blank'>Coding Pages</a></div>


<span class="post-meta-divider">|</span>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 <a class="theme-link" target="_blank" href="https://github.com/theme-next/hexo-theme-next">NexT.Muse</a> v6.0.4</div>




        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv" title="总访客量">
      <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
    </span>
  
  <span class="post-meta-divider"></span>
  
    <span class="site-pv" title="总访问量">
      <i class="fa fa-spin fa-globe"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
    </span>
  
</div>









        
      </div>
    </footer>

    

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
          <span id="scrollpercent"><span>0</span>%</span>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=6.0.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=6.0.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=6.0.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=6.0.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=6.0.4"></script>




  


  <script type="text/javascript" src="/js/src/love.js?v=6.0.4"></script>

  <script type="text/javascript" src="/js/src/console-log.js?v=6.0.4"></script>




  



	





  





  










  





  

  

  

  

  
  

  

  

  

  



  



</body>
</html>
